﻿using HtmlAgilityPack;
using SpiderStudy.Utils.Currency;
using System;
using System.Collections.Generic;

namespace SpiderStudy.Utils.Html
{
    internal class HtmlHelper
    {/// <summary>
     /// 加载HTML
     /// </summary>
     /// <param name="Htmlnode"></param>
     /// <returns></returns>
        public static HtmlDocument LoadHtml(String Htmlnode)
        {
            HtmlDocument doc = new HtmlDocument();
            try
            {
                doc.LoadHtml(Htmlnode);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
                PrintLog.Log("加载HTML错误 ");
            }
            return doc;
        }

        /// <summary>
        /// 需要一个htmldoc 和准备用来自定义的规则
        /// </summary>
        /// <param name="shtml"></param>
        /// <param name="custom_property"></param>
        /// <returns></returns>
        public static String RemoveUnsafe(String shtml)
        {
            var htmlDoc = LoadHtml(shtml);

            htmlDoc = ChlidernDelete(htmlDoc, "script");
            htmlDoc = ChlidernDelete(htmlDoc, "style");
            htmlDoc = ChlidernDelete(htmlDoc, "//comment()");

            return htmlDoc.DocumentNode.InnerHtml;
        }

        private static HtmlDocument ChlidernDelete(HtmlDocument htmlDoc, string local_path)
        {
            try
            {
                HtmlNodeCollection htmlNodes = htmlDoc.DocumentNode.SelectNodes(local_path);
                if (htmlNodes != null)
                    foreach (HtmlNode comment in htmlNodes)
                    {
                        //(2) remove comment node itself
                        comment.ParentNode.RemoveChild(comment);
                    }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return htmlDoc;
        }
        /// <summary>
        /// 删除全部第三方属性
        /// </summary>
        /// <param name="Shtml"></param>
        /// <param name="Xpath"></param>
        /// <returns></returns>
        public string RemoveAllAttributes(string Shtml, string Xpath)
        {
            var HtmlDoc = LoadHtml(Shtml);
            var elementsWithStyleAttribute = HtmlDoc.DocumentNode.SelectNodes(Xpath);

            if (elementsWithStyleAttribute != null)
            {
                foreach (var element in elementsWithStyleAttribute)
                {
                    try
                    {
                        element.Attributes.Remove();
                    }
                    catch (Exception ex)
                    {
                        PrintLog.Log(ex);
                    }
                }
            }
            return HtmlDoc.DocumentNode.InnerHtml;
        }
        /// <summary>
        /// 删除Attribus
        /// </summary>
        /// <param name="html"></param>
        /// <param name="Xpath"></param>
        /// <param name="Attribus"></param>
        public  string RemoveAttributes(string Shtml, string Xpath, string Attribus)
        {
            var HtmlDoc = LoadHtml(Shtml);
            var elementsWithStyleAttribute = HtmlDoc.DocumentNode.SelectNodes(Xpath);

            if (elementsWithStyleAttribute != null)
            {
                foreach (var element in elementsWithStyleAttribute)
                {
                    try
                    {
                        element.Attributes[Attribus].Remove();
                    }
                    catch (Exception ex)
                    {
                        PrintLog.Log(ex);
                    }
                }
            }
            return HtmlDoc.DocumentNode.InnerHtml;
        }

        /// <summary>
        /// 删除指定位置的代码并且返回String结果
        /// </summary>
        /// <param name="Shtml"></param>
        /// <param name="Xpath"></param>
        /// <returns></returns>
        public static string RemoveNode(string Shtml, String Xpath)
        {
            var HtmlDoc = LoadHtml(Shtml);
            var divs = HtmlDoc.DocumentNode.SelectNodes(Xpath);

            if (divs != null)
            {
                foreach (var tag in divs)
                {
                    tag.Remove();
                }
            }
            return HtmlDoc.DocumentNode.InnerHtml;
        }

        /// <summary>
        /// 数据鉴别时防止代码污染，返回纯文字
        /// </summary>
        /// <param name="Shtml"></param>
        /// <returns></returns>
        internal static string OutText(string Shtml)
        {
            var HtmlDoc = LoadHtml(Shtml);
            return HtmlDoc.DocumentNode.InnerText;
        }

        /// <summary>
        /// 选择并且返回HtmlNodeCollection
        /// </summary>
        /// <param name="Htmlnode"></param>
        /// <param name="Xpath"></param>
        /// <returns></returns>
        public static HtmlNodeCollection SelectNodes(String Htmlnode, String Xpath)
        {
            HtmlNodeCollection NodeCollectionvalue = null;
            try
            {
                NodeCollectionvalue = LoadHtml(Htmlnode).DocumentNode.SelectNodes(Xpath);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
                PrintLog.Log("选择器错误");
            }
            return NodeCollectionvalue;
        }

        /// <summary>
        /// 获取htmlNodeCollection里的inntext值
        /// </summary>
        /// <param name="Htmlnode"></param>
        /// <param name="Xpath"></param>
        /// <returns></returns>
        public static List<String> GetHtmlValueList(String Htmlnode, String Xpath)
        {
            List<string> ValueList = new List<string>();
            HtmlNodeCollection htmlNodeCollection = SelectNodes(Htmlnode, Xpath);
            if (htmlNodeCollection != null)

                foreach (HtmlNode SingleNode in htmlNodeCollection)
                {
                    ValueList.Add(SingleNode.InnerHtml);
                }
            return ValueList;
        }

        /// <summary>
        /// 获取htmlNodeCollection里的inntext值
        /// </summary>
        /// <param name="Htmlnode"></param>
        /// <param name="Xpath"></param>
        /// <returns></returns>
        public static List<String> GetTextValueList(String Htmlnode, String Xpath)
        {
            List<string> ValueList = new List<string>();
            HtmlNodeCollection htmlNodeCollection = SelectNodes(Htmlnode, Xpath);
            if (htmlNodeCollection != null)

                foreach (HtmlNode SingleNode in htmlNodeCollection)
                {
                    ValueList.Add(SingleNode.InnerText);
                }
            return ValueList;
        }/// <summary>

        /// 获取A标签里的href属性值
        /// </summary>
        /// <param name="Htmlnode"></param>
        /// <param name="Xpath"></param>
        /// <returns></returns>

        public static List<String> GetLinkVlaueList(String Htmlnode, String Xpath)
        {
            List<string> ValueList = new List<string>();
            HtmlNodeCollection htmlNodeCollection = SelectNodes(Htmlnode, Xpath);
            if (htmlNodeCollection != null)
                foreach (HtmlNode SingleNode in htmlNodeCollection)
                {
                    try
                    {
                        ValueList.Add(SingleNode.Attributes["href"].Value);
                    }
                    catch (Exception ex)
                    {
                        PrintLog.Log(ex);
                        PrintLog.Log("获取超链接错误");
                    }
                }
            return ValueList;
        }/// <summary>

        /// 获取图片src的辅助方法
        /// </summary>
        /// <param name="Htmlnode"></param>
        /// <param name="Xpath"></param>
        /// <returns></returns>

        public static List<String> GetImgSrcVlaueList(String Htmlnode, String Xpath)
        {
            List<string> ValueList = new List<string>();
            HtmlNodeCollection htmlNodeCollection = SelectNodes(Htmlnode, Xpath);
            if (htmlNodeCollection != null)
                foreach (HtmlNode SingleNode in htmlNodeCollection)
                {
                    try
                    {
                        ValueList.Add(SingleNode.Attributes["src"].Value);
                    }
                    catch (Exception ex)
                    {
                        PrintLog.Log(ex);
                        PrintLog.Log("获取超链接错误");
                    }
                }
            return ValueList;
        }

        /// <summary>
        /// 获取图片src的辅助方法
        /// </summary>
        /// <param name="Htmlnode"></param>
        /// <param name="Xpath"></param>
        /// <returns></returns>

        public static List<String> GetAttributesVlaueList(String Htmlnode, String Attributes, String Xpath)
        {
            List<string> ValueList = new List<string>();
            HtmlNodeCollection htmlNodeCollection = SelectNodes(Htmlnode, Xpath);

            if (htmlNodeCollection != null)
                foreach (HtmlNode SingleNode in htmlNodeCollection)
                {
                    try
                    {
                        ValueList.Add(SingleNode.Attributes[Attributes].Value);
                    }
                    catch (Exception ex)
                    {
                        PrintLog.Log(ex);
                        PrintLog.Log("获取GetAttributesVlaueList错误");
                    }
                }
            else
                Console.WriteLine("null");
            return ValueList;
        }

        /// <summary>
        /// 获取第一个值，因为htmlnode会崩溃所以要单独抽出来做
        /// </summary>
        /// <param name="list">需要liststing</param>
        /// <returns></returns>
        public static String GetFirstValue(List<String> list)
        {
            String value = "";
            foreach (string line in list)
            {
                value = line;
                break;
            }
            return value;
        }
    }
}